package stack;

import entity.Link.ListNode;

public class leetcode234回文链表 {
    public boolean isPalindrome(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode p = head;
        ListNode p1 = revert(slow);
        while (p1 != null) {
            if (p.val != p1.val) {
                return false;
            }
            p = p.next;
            p1 = p1.next;
        }
        return true;
    }
    public ListNode revert(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode pre = null;
        ListNode post = head.next;
        while (head != null) {
            head.next = pre;
            pre = head;
            head = post;
            if (post != null) {
                post = post.next;
            }
        }
        return pre;
    }
}
